package com.danicsoft.daide.command;

import com.danicsoft.daide.token.TokenGroup;
import com.danicsoft.daide.token.TokenMap.Commands;

/**
 * Implements the SVE command. From Eric Wald's <a
 * href="http://pydip.brainshell.org/docs/syntax.html">protocol specification</a>,
 * v0.14:
 * <h4><a name="SVE">server_command = SVE</a> ('gamename')</h4>
 * <p>
 * The server can send this at any point. It is a request for the client to save
 * the current game with the name given. <b>gamename</b> will never be more
 * than 8 characters, and can include only letters, numbers and underscore. Game
 * names are not case sensitive. The client should save everything it will need
 * in order to be able to resume the game at a later date. Once the game is
 * saved, the client should respond with <b>YES (SVE ('gamename'))</b>. If
 * there is an error saving the game, then it should respond with <b>REJ (SVE
 * ('gamename')).</b> Once the game has been saved, the client should continue
 * playing.
 * </p>
 * 
 * @author Daniel Yule (daniel.yule@gmail.com)
 * @version 1.0
 */
public class SaveCommand extends Command {

	/**
	 * The name of the game we are saving.
	 */
	private String gameName;

	/**
	 * Constructs a new <code>SaveCommand</code> from the tokens given.
	 * 
	 * @param tokens
	 *        The tokens to load this SaveCommand from. They should be a group
	 *        with a single <code>TextToken</code> specifying the name of the
	 *        game to save.
	 * @throws ParseException
	 *         If there was a problem reading the tokens.
	 */
	public SaveCommand(TokenGroup tokens) throws ParseException {
		super(tokens);
	}

	/**
	 * Construct a new <code>SaveCommand</code> for the game name given.
	 * 
	 * @param gameName
	 *        The name of the game to save
	 */
	public SaveCommand(String gameName) {
		this.gameName = gameName;
	}

	@Override
	public byte getType() {
		return Commands.SVE;
	}

	@Override
	public String toString() {
		return "Saving: " + gameName;
	}

	public void doIO(TokenIO tokenIO, TokenGroup tokens) throws ParseException {
		gameName = tokenIO.doText(tokenIO.subGroup(tokens), gameName);

	}

	/**
	 * Gets the name of the game we are saving.
	 * 
	 * @return The name of the game we are saving.
	 */
	public String getGameName() {
		return gameName;
	}

}
